package com.itheima.reggie.exception;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLIntegrityConstraintViolationException;
@RestControllerAdvice
public class GlobalException {
    //全局异常处理
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHeader(SQLIntegrityConstraintViolationException e){
        if(e.getMessage().contains("Duplicate entry")){
            String[] split = e.getMessage().split(" ");
            return R.error(split[2]+"已经存在");
        }
        return R.error("未知错误");
    }



    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHeader(CustomException e){
        return R.error(e.getMessage());
    }
}